<?php
/**
 * 产品模块
 *
 * 产品模块controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\ProductsService\ProductsService;
use Illuminate\Support\Facades\Log;
use App\Services\ModifyResourcePriceRecordsService\ModifyResourcePriceRecordsService;
use Response;
use DB;

class ProductController extends Controller
{
    protected $productService;
    public function __construct(ProductsService $productsService)
    {
        $this->productService = $productsService;
    }

    /**
     * 获取所有的产品
     */
    public function postAllProducts(Request $request){
        $user_id = $request->input('user_id');
        $conditions = trim($request->input('conditions'));
        $per_page = intval($request->input('per_page')) > 0 ? intval($request->input('per_page')) : 10;
        $order = intval($request->input('order'));
        if($order == 1){
            $order = 'preferential_price';
        }elseif($order == 2){
            $order = 'collect_num';
//        }elseif($order == 3){
//            $order = 'customize';
        }else{
            $order = 'created_at';
        }
        //$order = $request->input('order') == null ? 'created_at' : trim($request->input('order'));// created_at preferential_price dig_num customize(自定义) collect_num
        //$sort = $request->input('sort') == 'desc' ? 'desc' : 'asc';
        $sort = intval($request->input('sort')) == 0 ? 'desc' :  'asc';
        $product_temp_category_id = intval($request->input('product_temp_category_id'));
        if(!empty($conditions)){
            //$conditions = trim($request->input('conditions'));
            $conditions = json_decode($conditions);
            if(is_null($conditions)){
                return Response::json($this->productService->createResult('M08013',(object)array()));
            }
            //$order = is_null($conditions) || empty($conditions) ? 'created_at' : 'customize';
        }
        //获取所有的产品
        $result = $this->productService->getList($product_temp_category_id,$per_page,$order,$sort,$conditions,$user_id);
        return Response::json($result);
    }

    /**
     * 根据作品集获取作品
     * @param Request $request
     * @return mixed
     */
    public function postGetProductsByCollectionId(Request $request){
        $check_data = array('collection_id'=>'required|alpha_dash|max:32','user_id'=>'required|integer');
        $v = $this->productService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('collection_id')){
                $resultMes = $this->productService->createResult('M08003',(object)array());
            } else {
                $resultMes = $this->productService->createResult('M08004',(object)array());
            }
            return Response::json($resultMes);
        }
        $collection_id = trim($request->input('collection_id'));
        $user_id = intval($request->input('user_id'));
        $per_page = intval($request->input('per_page')) > 0 ? intval($request->input('per_page')) : 10;
        $result = $this->productService->getProductsByCollectionId($user_id,$collection_id,'collections',$per_page);
        return Response::json($result);
    }

    /**
     * 编辑或者上架作品
     * @param Request $request
     * @return mixed
     */
    public function postModifyProduct(Request $request){
        $check_data = array(
            'product_id'=>'required|alpha_dash|max:32',
            'user_id'=>'required|integer'
        );
        $v = $this->productService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('product_id')){
                $resultMes = $this->productService->createResult('M08005',(object)array());
            }else {
                $resultMes = $this->productService->createResult('M08004',(object)array());
            }
            return Response::json($resultMes);
        }
        $result = $this->productService->getModifyProduct($request->input('user_id'),$request->input('product_id'));
        return Response::json($result);
    }

    /**
     * 上下架
     * @param Request $request
     * @return mixed
     */
    public function postShelves(Request $request){
        $check_data = array(
            'product_id'=>'required|alpha_dash|max:32',
            'user_id'=>'required|integer',
            'user_collection_id' => 'alpha_dash|max:32',
            'overflow_price'=>'numeric|min:0',
            'stock'=>'integer',
            'off_shelves_time'=>'numeric|min:0',
            'type' => 'required|in:0,1,2'
        );
        $v = $this->productService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('product_id')){
                $resultMes = $this->productService->createResult('M08005',(object)array());
            }elseif($v->errors()->first('user_id')) {
                $resultMes = $this->productService->createResult('M08004',(object)array());
            }elseif($v->errors()->first('overflow_price')){
                $resultMes = $this->productService->createResult('M08006',(object)array());
            }elseif($v->errors()->first('stock')){
                $resultMes = $this->productService->createResult('M08006',(object)array());
            }elseif($v->errors()->first('type')){
                $resultMes = $this->productService->createResult('M08015',(object)array());
            }elseif($v->errors()->first('user_collection_id')){
                $resultMes = $this->productService->createResult('M08003',(object)array());
            }else{
                $resultMes = $this->productService->createResult('M08008',(object)array());
            }
            return Response::json($resultMes);
        }
        $product_id = trim($request->input('product_id'));
        $user_id = intval($request->input('user_id'));
        $overflow_price = $request->input('overflow_price');
        $stock = $request->input('stock');
        $off_shelves_time = $request->input('off_shelves_time');
        $type = $request->input('type');
        $user_collection_id  = $request->input('user_collection_id');
        $input = array('product_id'=>$product_id,'user_id'=>$user_id,'overflow_price'=>$overflow_price,'stock'=>$stock,'off_shelves_time'=>$off_shelves_time,'type'=>$type,'user_collection_id'=>$user_collection_id);
        $result = $this->productService->shelvesProduct($input);
        return Response::json($result);
    }

    /**
     * 删除
     * @param Request $request
     * @return mixed
     */
    public function postDelete(Request $request){
        $check_data = array(
            'product_id'=>'required|alpha_dash|max:32',
            'user_id'=>'required|integer'
        );
        $v = $this->productService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('product_id')){
                $resultMes = $this->productService->createResult('M08005',(object)array());
            }else {
                $resultMes = $this->productService->createResult('M08004',(object)array());
            }
            return Response::json($resultMes);
        }
        $product_id = trim($request->input('product_id'));
        $user_id = intval($request->input('user_id'));
        $result = $this->productService->deleteProduct(array('product_id'=>$product_id,'user_id'=>$user_id));
        return Response::json($result);
    }

    /**
     * 产品详情
     * @param Request $request
     * @return mixed
     */
    public function postProductDetail(Request $request){
        $check_data = array(
            'product_id'=>'required|alpha_dash|max:32',
            'user_id'=>'integer'
        );
        $v = $this->productService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('product_id')){
                $resultMes = $this->productService->createResult('M08005',(object)array());
            }else {
                $resultMes = $this->productService->createResult('M08004',(object)array());
            }
            return Response::json($resultMes);
        }
        $product_id = trim($request->input('product_id'));
        $user_id = intval($request->input('user_id'));
        $platform = $request->input('platform');
        $result = $this->productService->get_product_detail(['product_id'=>$product_id,'user_id'=>$user_id,'platform'=>$platform]);
        return Response::json($result);
    }

    /**
     * 获取产品以及其相关颜色产品详情
     * @param Request $request
     * @return mixed
     */
    public function postProductsDetail(Request $request){
        $check_data = array(
            'product_id'=>'required|alpha_dash|max:32',
            'user_id'=>'integer'
        );
        $v = $this->productService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('product_id')){
                $resultMes = $this->productService->createResult('M08005',(object)array());
            }else {
                $resultMes = $this->productService->createResult('M08004',(object)array());
            }
            return Response::json($resultMes);
        }
        $product_id = trim($request->input('product_id'));
        $user_id = intval($request->input('user_id'));
        $platform = $request->input('platform');
        $result = $this->productService->get_products_detail(['product_id'=>$product_id,'user_id'=>$user_id,'platform'=>$platform]);
        return Response::json($result);
    }

    /**
     * 下单所需参数
     * @param Request $request
     * @return mixed
     */
    public function postProductParamters(Request $request){
        $check_data = array(
            'product_id'=>'required|alpha_dash|max:32',
            'user_id'=>'integer'
        );
        $v = $this->productService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('product_id')){
                $resultMes = $this->productService->createResult('M08005',(object)array());
            }else {
                $resultMes = $this->productService->createResult('M08004',(object)array());
            }
            return Response::json($resultMes);
        }
        //获取下单参数
        $user_id = $request->input('user_id');
        $product_id = trim($request->input('product_id'));
        $result = $this->productService->get_order_product_parameters($user_id,$product_id);
        return Response::json($result);
    }

    /**
     * 获取用户所有上架产品
     * @param Request $request
     */
    public function postShelvesProducts(Request $request){
        $user_id = intval($request->input('user_id'));
        $per_page = intval($request->input('per_page')) > 0 ? intval($request->input('per_page')) : 10;
        $result = $this->productService->get_shelves_products($user_id,$per_page);
        return Response::json($result);
    }

    /**
     * 获取用户所有上架产品
     * @param Request $request
     */
    public function postOutShelvesProducts(Request $request){
        $user_id = intval($request->input('user_id'));
        $per_page = intval($request->input('per_page')) > 0 ? intval($request->input('per_page')) : 10;
        $result = $this->productService->get_out_shelves_products($user_id,$per_page);
        return Response::json($result);
    }

    /**
     * 设计师或者品牌商创作作品(新版)
     * @param Request $request
     * @return mixed
     */
    public function postCreation(Request $request){
        $user_id = intval($request->input('user_id'));
        $product_templates = trim($request->input('product_templates'));
        $platform = trim($request->input('platform'));
        $screen_size = trim($request->input('screen_size'));
        $app_version = trim($request->input('app_version'));
        $result = $this->productService->checkParamters(array('user_id'=>$user_id,'product_templates'=>$product_templates,
            'platform'=>$platform,'screen_size'=>$screen_size,'app_version'=>$app_version),1);
        if($result['state'] != 'M00000'){
            return Response::json($result);
        }
        $result = $this->productService->create_product_preview($result['result']);
        return Response::json($result);
    }

    /**
     * 普通用户设计作品(新版)
     * @param Request $request
     * @return mixed
     */
    public function postCommonUserCreation(Request $request){
        $user_id = intval($request->input('user_id'));
        $product_templates = trim($request->input('product_templates'));
        $platform = trim($request->input('platform'));
        $screen_size = trim($request->input('screen_size'));
        $app_version = trim($request->input('app_version'));
        $result = $this->productService->checkParamters(array('user_id'=>$user_id,'product_templates'=>$product_templates,
            'platform'=>$platform,'screen_size'=>$screen_size,'app_version'=>$app_version),0);
        if($result['state'] != 'M00000'){
            return Response::json($result);
        }
        $result = $this->productService->create_product_preview($result['result']);
        return Response::json($result);
    }

    /**
     * 发布产品(设计师)
     * @param Request $request
     * @return array
     */
    public function postSavePublication(Request $request){
        $user_id = intval($request->input('user_id'));
        $products = trim($request->input('products'));
        $platform = trim($request->input('platform'));
        $product_draft_md5 = trim($request->input('product_draft_md5'));
        $products = json_decode($products);
        if (is_null($products) || empty($products)) {
            return $this->productService->createResult('M08015', (object)array());
        }
        $result = $this->productService->save_product(array('user_id'=>$user_id,'products'=>$products,'platform'=>$platform,'product_draft_md5'=>$product_draft_md5));
        return Response::json($result);
    }

    /**
     * 发布产品(普通用户)
     * @param Request $request
     * @return array
     */
    public function postCommonUserSavePublication(Request $request){
        $user_id = intval($request->input('user_id'));
        $products = trim($request->input('products'));
        $platform = trim($request->input('platform'));
        $product_draft_md5 = trim($request->input('product_draft_md5'));
        $products = json_decode($products);
        if (is_null($products) || empty($products)) {
            return $this->productService->createResult('M08015', (object)array());
        }
        $result = $this->productService->common_save_product(array('user_id'=>$user_id,'products'=>$products,'platform'=>$platform,'product_draft_md5'=>$product_draft_md5));
        return Response::json($result);
    }

    /**
     * 获取用户作品列表
     * @param Request $request
     * @return mixed
     */
    public function postList(Request $request){
        $user_id = intval($request->input('user_id'));
        $per_page = intval($request->input('per_page')) > 0 ? intval($request->input('per_page')) : 10;
        $result = $this->productService->get_list_by_user_id($user_id,$per_page);
        return Response::json($result);
    }

    /**
     * 获取普通用户的产品详情
     * @param Request $request
     * @return mixed
     */
    public function postCommonUserProductDetail(Request $request){
        $check_data = array(
            'product_id'=>'required|alpha_dash|max:32',
            'user_id'=>'integer'
        );
        $v = $this->productService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('product_id')){
                $resultMes = $this->productService->createResult('M08005',(object)array());
            }else {
                $resultMes = $this->productService->createResult('M08004',(object)array());
            }
            return Response::json($resultMes);
        }
        $product_id = trim($request->input('product_id'));
        $user_id = intval($request->input('user_id'));
        $result = $this->productService->get_product_detail_list(['product_id'=>$product_id,'user_id'=>$user_id]);
        return Response::json($result);
    }

    /**
     * 保存草稿
     * @Author    zhouchao
     * @DateTime  2017-03-06
     * @copyright 2016 QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     Request $request
     * @return    mixed
     */
    public function postDrafts(Request $request)
    {
        $user_id = intval($request->input('user_id'));
        $product_templates = trim($request->input('product_templates'));
        $platform = trim($request->input('platform'));
        $screen_size = trim($request->input('screen_size'));
        $app_version = trim($request->input('app_version'));
        $product_draft_md5 = trim($request->input('product_draft_md5'));
        $result = $this->productService->checkParamters(array('user_id'=>$user_id,'product_draft_md5'=>$product_draft_md5,'product_templates'=>$product_templates,
            'platform'=>$platform,'screen_size'=>$screen_size,'app_version'=>$app_version),2);//不分用户
        if($result['state'] != 'M00000'){
            return Response::json($result);
        }
        $result = $this->productService->save_product_draft_preview($result['result']);
        return Response::json($result);
    }

    /**
     * 获取草稿列表
     * @Author    zhouchao
     * @DateTime  2017-03-07
     * @copyright 2016 QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     Request
     * @return    mixed
     */
    public function postDraftList(Request $request)
    {
        $user_id = intval($request->input('user_id'));
        $per_page = intval($request->input('per_page')) > 0 ? intval($request->input('per_page')) : 10;
        $result = $this->productService->get_draft_list($user_id, $per_page);
        return Response::json($result);
    }

    /**
     * 删除草稿
     * @Author    zhouchao
     * @DateTime  2017-03-07
     * @copyright 2016 QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     Request
     * @return    mixed
     */
    public function postDeleteDraft(Request $request)
    {
        $user_id = intval($request->input('user_id'));
        $product_draft_id = intval($request->input('product_draft_id'));
        $result = $this->productService->delete_draft($user_id, $product_draft_id);
        return Response::json($result);
    }

    /**
     * 草稿上架
     * @Author    zhouchao
     * @DateTime  2017-03-07
     * @copyright 2016 QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     Request
     * @return    mixed
     */
    public function postDraftShelves(Request $request)
    {
        $user_id = intval($request->input('user_id'));
        $product_draft_id = intval($request->input('product_draft_id'));
        $result = $this->productService->draft_shelves($user_id, $product_draft_id);
        return Response::json($result);
    }

    /**
     * 草稿编辑
     * @Author    zhouchao
     * @DateTime  2017-03-07
     * @copyright 2016 QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     Request
     * @return    mixed
     */
    public function postDraftEdit(Request $request)
    {
        $user_id = intval($request->input('user_id'));
        $product_draft_id = intval($request->input('product_draft_id'));
        $result = $this->productService->draft_edit($user_id, $product_draft_id);
        return Response::json($result);
    }
}
